
package com.googlecode.xmltojava.scriptparser;

import java_cup.runtime.*;
import java.io.IOException;

import static com.googlecode.xmltojava.scriptparser.ParserSym.*;
import com.googlecode.xmltojava.ScriptItem;
%%

%class LexerLex
%unicode
%line
%column

%public
%final
// %abstract

%cupsym ParserSym
%cup
// %cupdebug

%init{
	// TODO: code that goes to constructor
%init}

%{
	private Symbol sym(int type)
	{
		return sym(type, yytext());
	}

	private Symbol sym(int type, Object value)
	{
		return new Symbol(type, yyline, yycolumn, value);
	}

	
%}

tLF         =   [\n\r]*
tSP         =   [\ \t]*
tText       =   [^<>]*
tRegExp     =   <{tSP}{tText}{tSP}>

//Non faccio distinzioni tra giorno e mese: in questo modo posso rimanere generico,
//e considerare come valide anche le date espresse in formati anglosassoni con nome
//e mese invertiti. Idem per quanto riguarda l'anno... non specifico il numero di cifre,
//in modo da poter gestire sia date in formato completo (10/12/2009) che in formato 
//abbreviato (10/12/09)
tDate       =   ([0-3]?[0-9]\/[0-3]?[0-9]\/[0-9]*)|Today({tSP}[+-]{tSP}[0-9]*)?
tMinus      =   \-
tComma      =   \,

//Keywords
tkIN        =   IN
tkAFTER     =   AFTER
tkBEFORE    =   BEFORE
tkWD        =   WITH{tSP}DATES
tkShowAS    =   SHOW{tSP}AS
tkMergeW    =   MERGE{tSP}WITH

tkBRKOpen   =   \(
tkBRKClosed =   \)
tkAND       =   AND
tkOR        =   OR

tkEnT       =   [Tt]itle|TITLE
tkEnS       =   [Ss]ummary|SUMMARY
tkEnD		=   [Dd]escription|DESCRIPTION
tkEnO		=	[Oo]rganizer|ORGANIZER

tkEoWeek    =   WEEK|[Ww]eek
tkEoMonth   =   MONTH|[Mm]onth
tkEoYear    =   YEAR|[Yy]ear

tkEpList    =   LIST
tkEpGrid    =   GRID

%%

{tRegExp}				{ System.out.print("#REGEXP#"); 			return new Symbol(ParserSym.kREGEXP, new ScriptItem(yytext())); }
{tkBRKOpen}     		{ System.out.print("["); 					return new Symbol(ParserSym.kBRKOPEN);}
{tkBRKClosed}    		{ System.out.print("]"); 					return new Symbol(ParserSym.kBRKCLOSE);}
{tkIN}        		 	{ System.out.print("SEP("+yytext()+")"); 	return new Symbol(ParserSym.kIN);}
{tkAFTER}				{ System.out.print("SEP("+yytext()+")"); 	return new Symbol(ParserSym.kAFTER);}
{tkBEFORE}				{ System.out.print("SEP("+yytext()+")");	return new Symbol(ParserSym.kBEFORE);}
{tkWD}					{ System.out.print("WD");					return new Symbol(ParserSym.kWD);}
{tkShowAS} 				{ System.out.print("ShowAS");				return new Symbol(ParserSym.kSHOWAS);}
{tkMergeW}				{ System.out.print("MergeW");				return new Symbol(ParserSym.kMW);}
{tkAND}					{ System.out.print("AND");					return new Symbol(ParserSym.kAND);}
{tkOR}					{ System.out.print("OR");					return new Symbol(ParserSym.kOR);}

{tkEnT}|{tkEnS}|{tkEnD}|{tkEnO}   { System.out.print("EN");			return new Symbol(ParserSym.kEN, new ScriptItem(yytext().toUpperCase()));}
{tkEoWeek}|{tkEoMonth}|{tkEoYear} { System.out.print("EO");			return new Symbol(ParserSym.kEO, new ScriptItem(yytext()));}
{tkEpList}|{tkEpGrid}   { System.out.print("EP");					return new Symbol(ParserSym.kEP, new ScriptItem(yytext()));}

{tDate}  				{ System.out.print("DATE");					return new Symbol(ParserSym.kDATE, new ScriptItem(yytext()));}
{tMinus}		 		{ System.out.print("-");					return new Symbol(ParserSym.kMINUS);}
{tComma}				{ System.out.print(",");					return new Symbol(ParserSym.kCOMMA);}

{tSP}					{ }
{tLF}					{ }



.						{ /* Gestione "silente" degli errori */ }
